//
//  postPhotoViewController.m
//  YuErmuYing
//
//  Created by Tiancheng on 16/8/24.
//  Copyright © 2016年 Tiancheng. All rights reserved.
//

#import "postPhotoViewController.h"
#import "Macros.h"
#import "postPhoto.h"
#import "photoHeaderView.h"
#import "HttpRequest.h"
#import "photoCustomTableViewCell.h"
#import "NSString+NSDateToNSString.h"
#import "UILabel+LabelHeightAndWidth.h"
#import "UIImageView+WebCache.h"
#import "joinPhotoViewController.h"
#import "CustomButton.h"
#import "SVProgressHUD.h"

@interface postPhotoViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView00;
@property (weak, nonatomic) IBOutlet UIView *headerView;
@property (weak, nonatomic) IBOutlet UIView *headerView01;
@property(nonatomic,retain)UIButton *shouBtn;

//地址01
@property(nonatomic,copy)NSString*urlString1;
//用数组接收网络数据json_Array1,json_Array1
@property(nonatomic,copy)NSMutableArray*json_Array1;
@property(nonatomic,copy)NSMutableArray*json_Array2;

//马上参与
@property(nonatomic,strong)UIButton* joinBtn;
@property(nonatomic,strong)photoCustomTableViewCell *myCell;

@property(nonatomic,strong)NSMutableDictionary *dictionary1;
@property(nonatomic,strong)NSMutableDictionary *dictionary2;
@property(nonatomic,strong)NSMutableDictionary *dictionary3;
@property(nonatomic,copy)NSMutableArray *picArray;
//内容高度height
@property(nonatomic,assign)CGFloat height;

@property(nonatomic,retain)UIView *backView;
@end

@implementation postPhotoViewController
@synthesize json_Array1,json_Array2,dictionary1,dictionary2,dictionary3,picArray,height;
-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden=YES;
    self.tabBarController.tabBar.hidden=YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
//    NSLog(@"screenWidth=%f",screenWidth);
//    NSLog(@"screenHeight=%f",screenHeight);

    self.tableView00.delegate=self;
    self.tableView00.dataSource=self;
    self.headerView.backgroundColor=[UIColor colorWithRed:244/255.0 green:143/255.0 blue:177/255.0 alpha:1.0];
    self.headerView01.backgroundColor=[UIColor colorWithRed:240/255.0 green:98/255.0 blue:146/255.0 alpha:1.0];
    self.joinBtn=[[UIButton alloc]initWithFrame:CGRectMake(0, screenHeight-40, screenWidth, 40)];
    self.joinBtn.backgroundColor=[UIColor colorWithRed:240/255.0 green:98/255.0 blue:146/255.0 alpha:1.0];
    [self.joinBtn setTitle:@"马上参与" forState:UIControlStateNormal];
    [self.joinBtn addTarget:self action:@selector(join:) forControlEvents:UIControlEventTouchUpInside];
    [self.joinBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.view addSubview:self.joinBtn];
    json_Array1=[NSMutableArray array];
    json_Array2=[NSMutableArray array];
    picArray=[NSMutableArray array];
    [self updateDataFromNet];
    self.backView=[[UIView alloc]initWithFrame:CGRectMake(0, 64, screenWidth, screenHeight-64)];
    self.backView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:self.backView];
    [SVProgressHUD showWithStatus:@"加载中请稍后..."];

}
-(void)updateDataFromNet
{
//    postPhoto*pp=[postPhoto shardeManager];
    //user_id=@"8";  active=   1是点过,0是没点过
    NSMutableDictionary *parameters=[NSMutableDictionary dictionary];
    parameters[@"user_id"]=@"8";
    self.urlString1=[NSString stringWithFormat:@"http://112.74.114.1/parenting/Api/Active/find_passphoto"];
    HttpRequest *httpRequest=[HttpRequest sharedInstance];
    [httpRequest postWithURLString:self.urlString1 parameters:parameters success:^(id responseObject)
     {
         id json = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
         //获取数组
         NSDictionary *dic=[json objectForKey:@"data"];
         json_Array1=[dic objectForKey:@"data1"];
         json_Array2=[dic objectForKey:@"data2"];
//         NSLog(@"json_Array2=%@",json_Array2);

         if (json_Array1.count>0)
         {
             [self dismiss];
//             pp.contentString=[json_Array1[0] objectForKey:@"active_content"];
//             pp.nameString=[json_Array1[0] objectForKey:@"active_title"];
//             pp.picString=[json_Array1[0] objectForKey:@"active_pic"];
//             pp.timeString=[json_Array1[0] objectForKey:@"active_time"];
////             NSLog(@"pp.timeString00=%@",pp.timeString);
//             photoHeaderView*phv=[photoHeaderView appViewWithApp:pp];
//             phv.frame=CGRectMake(0, 0, screenWidth, 300);
//
//             [self.tableView00 setTableHeaderView:phv];
//             [self.tableView00 reloadData];
         }
//         NSLog(@"json=%@",json);
     } failure:^(NSError *error) {
         NSLog(@"error=%@",error);
     }];
}
- (void)dismiss
{
    [self.backView removeFromSuperview];
    [SVProgressHUD dismiss];
    [self setTableSource];
    
}
-(void)setTableSource
{
    postPhoto*pp=[postPhoto shardeManager];
    pp.contentString=[json_Array1[0] objectForKey:@"active_content"];
    pp.nameString=[json_Array1[0] objectForKey:@"active_title"];
    pp.picString=[json_Array1[0] objectForKey:@"active_pic"];
    pp.timeString=[json_Array1[0] objectForKey:@"active_time"];
    //             NSLog(@"pp.timeString00=%@",pp.timeString);
    photoHeaderView*phv=[photoHeaderView appViewWithApp:pp];
    phv.frame=CGRectMake(0, 0, screenWidth, 300);
    [self.tableView00 setTableHeaderView:phv];
    [self.tableView00 reloadData];

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return json_Array2.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    postPhoto*pp=[postPhoto shardeManager];
    static NSString *CellIdentifier = @"cell";
    self.myCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(self.myCell == nil)
    {
       self.myCell =[[[NSBundle mainBundle] loadNibNamed:@"photoCustomTableViewCell" owner:self options:nil]lastObject];
        picArray=[json_Array2[indexPath.row] objectForKey:@"pic"];
//        NSLog(@"picArray=%@",picArray);
        pp.headerContentString=[json_Array2[indexPath.row] objectForKey:@"content"];
        //*******************************************
        pp.timeString01=[json_Array2[indexPath.row] objectForKey:@"upload_time"];
//        NSLog(@"pp.timeString=%@",pp.timeString);
        pp.headerImageString=[json_Array2[indexPath.row] objectForKey:@"user_pic"];
        pp.headerNameString=[json_Array2[indexPath.row] objectForKey:@"user_username"];
        pp.headerNumString=[json_Array2[indexPath.row] objectForKey:@"zan_num"];
        pp.headerIDString=[json_Array2[indexPath.row] objectForKey:@"id"];
        pp.activeIDString=[json_Array2[indexPath.row] objectForKey:@"active"];
        pp.user_idString=[json_Array2[indexPath.row] objectForKey:@"user_id"];
        NSUserDefaults *userDefaults=[NSUserDefaults standardUserDefaults];
        [userDefaults setObject:pp.user_idString forKey:pp.headerIDString];
        //更改小手的图片
//        int num=[pp.activeIDString intValue];
//        NSLog(@"num=%d",num);
        //id
        int idNum=[pp.headerIDString intValue];
        dictionary1=[NSMutableDictionary dictionary];
        dictionary2=[NSMutableDictionary dictionary];
//        dictionary3=[NSMutableDictionary dictionary];

        [dictionary1 setValue:pp.activeIDString forKey:pp.headerIDString];
        [dictionary2 setValue:pp.headerNumString forKey:pp.headerIDString];
//        self.myCell.headerBtn.tag=idNum;
//
//        if (num==1)
//        {
//            [self.myCell.headerBtn setImage:[UIImage imageNamed:@"likeClick"] forState:UIControlStateNormal];
//            
//        }
//        else if (num==0)
//        {
//            [self.myCell.headerBtn setImage:[UIImage imageNamed:@"like"] forState:UIControlStateNormal];
//        }
        self.myCell.headerNameLabel.text=pp.headerNameString;
//        self.myCell.headerNumLabel.text=pp.headerNumString;
        self.myCell.headerTimeLabel.text=[NSString StringNSDateToNSString:pp.timeString01];
        self.myCell.headerContentLabel.text=pp.headerContentString;
        
        //自适应cell的高度
        //self.myCell.headerContentLabel.frame.origin.y
        self.myCell.headerContentLabel.font = [UIFont systemFontOfSize:13];
         self.myCell.headerContentLabel.numberOfLines = 100;
         height=[UILabel getHeightByWidth:self.myCell.headerContentLabel.frame.size.width title:self.myCell.headerContentLabel.text font:self.myCell.headerContentLabel.font];
        
//        NSLog(@"heightheight=%f",height);
        //自定义headerContentLabel的frame
        self.myCell.headerContentLabel.frame = CGRectMake(5, self.myCell.headerContentLabel.frame.origin.y, screenWidth-5, height);
        self.myCell.headerImageView01.frame=CGRectMake(self.myCell.headerImageView01.frame.origin.x, height+60,self.myCell.headerImageView01.frame.size.width, self.myCell.headerImageView01.frame.size.height);
        self.myCell.headerImageView02.frame=CGRectMake(self.myCell.headerImageView02.frame.origin.x, height+60,self.myCell.headerImageView01.frame.size.width, self.myCell.headerImageView01.frame.size.height);
        self.myCell.headerImageView03.frame=CGRectMake(self.myCell.headerImageView03.frame.origin.x, height+60,self.myCell.headerImageView01.frame.size.width, self.myCell.headerImageView01.frame.size.height);
        self.myCell.headerTimeLabel.frame=CGRectMake(10, height+60+75+10,self.myCell.headerTimeLabel.frame.size.width, self.myCell.headerTimeLabel.frame.size.height);
        float shouBtn_x=self.myCell.headerTimeLabel.frame.origin.x+self.myCell.headerTimeLabel.frame.size.width;
        float shouBtn_y=self.myCell.headerTimeLabel.frame.origin.y;
        self.shouBtn=[[CustomButton alloc]initWithFrame:CGRectMake(shouBtn_x+50, shouBtn_y, 200, 30)];
        self.shouBtn.tag=idNum;
        [self.shouBtn setTitle:pp.headerNumString forState:UIControlStateNormal];
        int num=[pp.headerNumString intValue];
      
        if (num==0)
        {
            [self.shouBtn setImage:[UIImage imageNamed:@"like"] forState:UIControlStateNormal];
        }
        else
        {
            [self.shouBtn setImage:[UIImage imageNamed:@"likeClick"] forState:UIControlStateNormal];
        }
        [self.shouBtn addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.myCell.contentView addSubview:self.shouBtn];
//        self.myCell.headerNumLabel.frame=CGRectMake(screenWidth-80, height+60+75+10,self.myCell.headerNumLabel.frame.size.width, self.myCell.headerNumLabel.frame.size.height);
//        self.myCell.headerBtn.frame=CGRectMake(screenWidth-100, height+60+75+7,25,25);
//        [self.myCell.headerBtn addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
        CGRect rect = self.myCell.frame;
        rect.size.height = self.myCell.headerContentLabel.frame.size.height+185;
        self.myCell.frame = rect;
    }
    if (picArray.count>0)
    {
//        NSLog(@"11111");
        if (picArray.count==1)
        {
            pp.headerImageString01=picArray[0];
            NSURL* imagePath01 = [NSURL URLWithString:pp.headerImageString01];
            [self.myCell.headerImageView01 sd_setImageWithURL:imagePath01 placeholderImage:[UIImage imageNamed:@"default"]];
            self.myCell.headerImageView02.hidden=YES;
            self.myCell.headerImageView03.hidden=YES;
        }
        else if (picArray.count==2)
        {
            pp.headerImageString01=picArray[0];
            NSURL* imagePath01 = [NSURL URLWithString:pp.headerImageString01];
            [self.myCell.headerImageView01 sd_setImageWithURL:imagePath01 placeholderImage:[UIImage imageNamed:@"default"]];
            pp.headerImageString02=picArray[1];
            NSURL* imagePath02 = [NSURL URLWithString:pp.headerImageString02];
            [self.myCell.headerImageView02 sd_setImageWithURL:imagePath02 placeholderImage:[UIImage imageNamed:@"default"]];
            self.myCell.headerImageView03.hidden=YES;

        }
        else if (picArray.count==3)
        {
            pp.headerImageString01=picArray[0];
            NSURL* imagePath01 = [NSURL URLWithString:pp.headerImageString01];
            [self.myCell.headerImageView01 sd_setImageWithURL:imagePath01 placeholderImage:[UIImage imageNamed:@"default"]];
            pp.headerImageString02=picArray[1];
            NSURL* imagePath02 = [NSURL URLWithString:pp.headerImageString02];
            [self.myCell.headerImageView02 sd_setImageWithURL:imagePath02 placeholderImage:[UIImage imageNamed:@"default"]];
            pp.headerImageString03=picArray[2];
            NSURL* imagePath03= [NSURL URLWithString:pp.headerImageString03];
            [self.myCell.headerImageView03 sd_setImageWithURL:imagePath03 placeholderImage:[UIImage imageNamed:@"default"]];
        }


    }else
    {
        self.myCell.headerImageView01.hidden=YES;
        self.myCell.headerImageView02.hidden=YES;
        self.myCell.headerImageView03.hidden=YES;
    }
    
    NSURL* imagePath00 = [NSURL URLWithString:pp.headerImageString];
//    NSURL* imagePath01 = [NSURL URLWithString:pp.headerImageString01];
//    NSURL* imagePath02 = [NSURL URLWithString:pp.headerImageString02];
//    NSURL* imagePath03 = [NSURL URLWithString:pp.headerImageString03];
    //给一张默认图片，先使用默认图片，当图片加载完成后再替换
//    [self.myCell.headerImageView01 sd_setImageWithURL:imagePath01 placeholderImage:[UIImage imageNamed:@"default"]];
//    [self.myCell.headerImageView02 sd_setImageWithURL:imagePath02 placeholderImage:[UIImage imageNamed:@"default"]];
//    [self.myCell.headerImageView03 sd_setImageWithURL:imagePath03 placeholderImage:[UIImage imageNamed:@"default"]];
   [self.myCell.headerImageView sd_setImageWithURL:imagePath00 placeholderImage:[UIImage imageNamed:@"default"]];
    
    return self.myCell;
}
//    likeClick

-(void)onClick:(UIButton*)sender
{
//    UIButton *btn=sender;
    NSString *idString=[NSString stringWithFormat:@"%ld",sender.tag];
//    NSLog(@"idString=%@",idString);
    int num1=[[dictionary1 objectForKey:idString]intValue];
    int num2=[[dictionary2 objectForKey:idString]intValue];
//    NSString *strstr=[dictionary3 objectForKey:idString];
    NSUserDefaults *userDefaults=[NSUserDefaults standardUserDefaults];
    NSString *strstr=[userDefaults valueForKey:idString];
//    NSLog(@"strstr=%@",strstr);
    if (num1==0)
    {
        [sender setImage:[UIImage imageNamed:@"likeClick"] forState:UIControlStateNormal];
        [dictionary1 setObject:@"1" forKey:idString];
        num2++;
        NSString*zan_numString=[NSString stringWithFormat:@"%d",num2];
//        NSLog(@"zan_numString=%@",zan_numString);
        [dictionary2 setObject:zan_numString forKey:idString];
        NSString*active=@"1";
        [sender setTitle:zan_numString forState:UIControlStateNormal];
//        self.myCell.headerNumLabel.text=zan_numString;
//        NSLog(@"self.myCell.headerNumLabel.text=%@",self.myCell.headerNumLabel.text);
//        [self.tableView00 reloadData];
        [self getDataFromNetzan_num:zan_numString idStr:idString user_idStr:strstr activeString:active];
    }
    if (num1==1)
    {
        [sender setImage:[UIImage imageNamed:@"like"] forState:UIControlStateNormal];
        [dictionary1 setObject:@"0" forKey:idString];
        num2--;
        NSString*zan_numString=[NSString stringWithFormat:@"%d",num2];
        [sender setTitle:zan_numString forState:UIControlStateNormal];

//        self.myCell.headerNumLabel.text=zan_numString;
//        NSLog(@"self.myCell.headerNumLabel.text=%@",self.myCell.headerNumLabel.text);
        [dictionary2 setObject:zan_numString forKey:idString];
        NSString*active=@"0";
//        NSLog(@"active=%@",active);
        [self getDataFromNetzan_num:zan_numString idStr:idString user_idStr:strstr activeString:active];

    }
    
    
}
-(void)getDataFromNetzan_num:(NSString*)zan_num idStr
                     :(NSString*)idStr user_idStr:(NSString*)user_idStr activeString:(NSString*)activeString
{
    NSMutableDictionary *parameters=[NSMutableDictionary dictionary];
    parameters[@"user_id"]=user_idStr;
    parameters[@"zan_num"]=zan_num;
    parameters[@"id"]=idStr;
    parameters[@"active"]=activeString;

    NSString *urlString00=[NSString stringWithFormat:@"http://112.74.114.1/parenting/Api/Active/click_zan"];
    HttpRequest *httpRequest=[HttpRequest sharedInstance];
    [httpRequest postWithURLString:urlString00 parameters:parameters success:^(id responseObject)
     {
         id json = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
         //获取数组
         
         
//         NSLog(@"json=%@",json);
     } failure:^(NSError *error) {
         NSLog(@"error=%@",error);
     }];



}
-(void)join:(UIButton*)sender
{
    joinPhotoViewController *jpvc=[joinPhotoViewController new];
    [self.navigationController pushViewController:jpvc animated:YES];
}
//颜色恢复
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
    [[self.tableView00 cellForRowAtIndexPath:indexPath].contentView setBackgroundColor:[UIColor whiteColor]];
}
//cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//   self.myCell=[self tableView:tableView cellForRowAtIndexPath:indexPath];
//    self.myCell=[self tableview];
    return self.myCell.frame.size.height;
}
//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
//{
//    return 150;
//}
- (IBAction)goBack:(id)sender
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
